package com.example.administrator.student4;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周千遇 on 2016/11/19.
 */
public class AddAcitvity extends Activity {
    SQLiteDatabase db;
    EditText et_name,et_grade;
    Spinner sp_subject,sp_term;
    String name,grade,subject,term;
    Button add;
    String[] arrname;
    Myhandler myhandler;
    String[] arr_subject,arr_term = {"term one","term two"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addactivity);
        db = openOrCreateDatabase("student.db",MODE_PRIVATE,null);
        uiinit();
        myhandler = new Myhandler();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    arr_subject = WebUtil.subjectfind();
                    myhandler.sendEmptyMessage(0x222);
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = et_name.getText().toString();
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        try {
                            arrname=WebUtil.namefind(name);
                            myhandler.sendEmptyMessage(0x111);
                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }
    public void uiinit(){
        et_grade = (EditText) findViewById(R.id.grade);
        et_name = (EditText) findViewById(R.id.name);
        sp_subject = (Spinner) findViewById(R.id.subject);
        sp_term = (Spinner) findViewById(R.id.term);
        add = (Button) findViewById(R.id.add);

    }
    class Myhandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0x111){
                if(arrname.length == 0){
                    AlertDialog.Builder builder = new AlertDialog.Builder(AddAcitvity.this);
                    builder.setTitle("警告").setMessage("该学生不存在").setPositiveButton("确定",null).show();
                }
                else {
                    name = arrname[0];
                    AlertDialog.Builder builder = new AlertDialog.Builder(AddAcitvity.this);
                    builder.setTitle("选择学生").setSingleChoiceItems(arrname, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            name = arrname[which];
                        }
                    }).setNegativeButton("取消",null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            name = arrname[0];
                            grade = et_grade.getText().toString();
                            term = sp_term.getSelectedItem().toString();
                            subject = sp_subject.getSelectedItem().toString();
                            AlertDialog.Builder builder = new AlertDialog.Builder(AddAcitvity.this);
                            View view = LayoutInflater.from(AddAcitvity.this).inflate(R.layout.confirm,null);
                            TextView tv_name,tv_subject,tv_term,tv_grade;
                            tv_name = (TextView) view.findViewById(R.id.name);
                            tv_subject = (TextView) view.findViewById(R.id.subject);
                            tv_term = (TextView) view.findViewById(R.id.term);
                            tv_grade = (TextView) view.findViewById(R.id.grade);
                            tv_name.setText(name);
                            tv_subject.setText(subject);
                            tv_term.setText(term);
                            tv_grade.setText(grade);
                            builder.setTitle("确认信息").setView(view).setNegativeButton("取消",null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    final JSONObject json = new JSONObject();
                                    try {
                                        json.put("name",name);
                                        json.put("subject",subject);
                                        json.put("grade",grade);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    new Thread(){
                                       @Override
                                       public void run() {
                                           super.run();
                                           String path = "http://10.0.2.2:8080/andorid/grade?request=add";
                                           try {
                                               URL url = new URL(path);
                                               HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                               connection.setRequestMethod("POST");
                                               WebUtil.jsonOut(json,connection);
                                           } catch (IOException e) {
                                               e.printStackTrace();
                                           }
                                       }
                                   }.start();
                                    Toast.makeText(AddAcitvity.this,"添加成功",Toast.LENGTH_SHORT).show();
                                }
                            }).show();
                        }
                    }).show();
                }
            }
            if(msg.what == 0x222){
                ArrayAdapter<String> apsubject = new ArrayAdapter<String>(AddAcitvity.this,android.R.layout.simple_expandable_list_item_1,arr_subject);
                ArrayAdapter<String> apterm = new ArrayAdapter<String>(AddAcitvity.this,android.R.layout.simple_expandable_list_item_1,arr_term);
                sp_term.setAdapter(apterm);
                sp_subject.setAdapter(apsubject);
            }
        }
    }
}
